package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;
import java.util.Scanner;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

/**
 * 
 * classe che contiene le conclusioni finali, ulteriori foto,i dati del tecnico
 * esecutore, logo finale
 * 
 * @author tomaseo
 * 
 */
public class Conclusioni extends JPanel {

  // conclsusioni
  private JTextPane conclusioni = new JTextPane();

  // dati esecutore
  private JTextField nome = new JTextField() {
    @Override
    protected void paintComponent(Graphics g) {
      if (!isOpaque()) {
        int w = getWidth();
        int h = getHeight();
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(UIManager.getColor("TextField.background"));
        g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
        g2.setColor(Color.BLACK);
        g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
        g2.dispose();
      }
      super.paintComponent(g);
    }
  };

  private JTextField cognome = new JTextField() {
    @Override
    protected void paintComponent(Graphics g) {
      if (!isOpaque()) {
        int w = getWidth();
        int h = getHeight();
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(UIManager.getColor("TextField.background"));
        g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
        g2.setColor(Color.BLACK);
        g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
        g2.dispose();
      }
      super.paintComponent(g);
    }
  };

  private JTextField cell = new JTextField() {
    @Override
    protected void paintComponent(Graphics g) {
      if (!isOpaque()) {
        int w = getWidth();
        int h = getHeight();
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(UIManager.getColor("TextField.background"));
        g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
        g2.setColor(Color.BLACK);
        g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
        g2.dispose();
      }
      super.paintComponent(g);
    }
  };

  private JTextField mail = new JTextField() {
    @Override
    protected void paintComponent(Graphics g) {
      if (!isOpaque()) {
        int w = getWidth();
        int h = getHeight();
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(UIManager.getColor("TextField.background"));
        g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
        g2.setColor(Color.BLACK);
        g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
        g2.dispose();
      }
      super.paintComponent(g);
    }
  };

  private JPanel professionista = new JPanel();

  private JPanel photo = new JPanel();
  private JButton upload1 = new JButton(new ImageIcon(
      ClassLoader.getSystemResource("bupload.jpg")));
  private JButton upload2 = new JButton(new ImageIcon(
      ClassLoader.getSystemResource("bupload.jpg")));
  private JButton upload3 = new JButton(new ImageIcon(
      ClassLoader.getSystemResource("bupload.jpg")));
  private JButton upload4 = new JButton(new ImageIcon(
      ClassLoader.getSystemResource("bupload.jpg")));
  private String lastPath = "";
  private String pathIm1 = "";
  private String pathIm2 = "";
  private String pathIm3 = "";
  private String pathIm4 = "";
  private String sourceIm1 = "";
  private String sourceIm2 = "";
  private String sourceIm3 = "";
  private String sourceIm4 = "";

  private String directoryProject;

  private StartingWindow st;

  protected void paintComponent(Graphics g) {
    setOpaque(false);
    g.drawImage(
        (new ImageIcon(ClassLoader.getSystemResource("sfondo.jpg"))).getImage(),
        0, 0, null);
    super.paintComponent(g);
  }

  public Conclusioni(final StartingWindow st) {
    super();
    this.st = st;
    Properties pc = System.getProperties();
    String homePath = pc.getProperty("user.home");
    directoryProject = homePath + pc.getProperty("file.separator")
        + "ArchimedeSuper" + pc.getProperty("file.separator") + "aaAAzzZZ"
        + pc.getProperty("file.separator");
    conclusioni.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    conclusioni.setPreferredSize(new Dimension(700, 600));
    conclusioni.setFont(new Font("Arial", Font.PLAIN, 16));

    // copia incolla su conclusioni
    ClipboardPopupMenu copiaIncolla = ClipboardPopupMenu
        .installForComponent(conclusioni);
    copiaIncolla.addCopyFunction("Copia");
    copiaIncolla.addCutFunction("Taglia");
    copiaIncolla.addPasteFunction("Incolla");

    JLabel c = new JLabel("Conclusioni:");
    c.setFont(new Font("Arial", Font.BOLD, 20));
    setLayout(new MigLayout());
    professionista.setLayout(new MigLayout());
    professionista.setOpaque(false);
    nome.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
    cognome.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
    cell.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
    mail.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));

    nome.setOpaque(false);
    cognome.setOpaque(false);
    cell.setOpaque(false);
    mail.setOpaque(false);

    nome.setPreferredSize(new Dimension(170, 30));
    cognome.setPreferredSize(new Dimension(170, 30));
    cell.setPreferredSize(new Dimension(170, 30));
    mail.setPreferredSize(new Dimension(170, 30));

    professionista.add(new JLabel("NOME"));
    professionista.add(nome, "wrap");
    professionista.add(new JLabel("COGNOME"));
    professionista.add(cognome, "wrap");
    professionista.add(new JLabel("CELL"));
    professionista.add(cell, "span , wrap");
    professionista.add(new JLabel("MAIL"));
    professionista.add(mail, "span");
    JLabel p = new JLabel("Dati tecnico esecutore:");
    p.setFont(new Font("Arial", Font.BOLD, 20));

    photo.setLayout(new MigLayout());
    photo.setOpaque(false);
    JLabel infoUpload = new JLabel("Inserisci le tue foto : ");
    infoUpload.setFont(new Font("Arial", Font.BOLD, 20));

    // foto 1, se seleziono immagine abilito pulsante 2
    upload1.setBorder(null);
    upload1.setContentAreaFilled(false);
    upload1.setRolloverEnabled(true);
    upload1.setRolloverIcon(new ImageIcon(ClassLoader
        .getSystemResource("upload_icon.jpg")));
    upload1.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent arg0) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        ImagePreviewPanel preview = new ImagePreviewPanel();
        chooser.setAccessory(preview);
        chooser.addPropertyChangeListener(preview);
        lastPath = st.getPath();
        if (lastPath.equals(""))
          chooser.getCurrentDirectory();
        else
          chooser.setCurrentDirectory(new File(lastPath));
        int returnValue = chooser.showOpenDialog(upload1);

        if (returnValue == JFileChooser.APPROVE_OPTION) {
          String sourcePath = chooser.getSelectedFile().toString();
          st.setPath(sourcePath);
          lastPath = sourcePath;

          File source = new File(sourcePath);
          sourceIm1 = sourcePath;

          ImageIcon tmp = new ImageIcon(sourcePath);
          tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
              Image.SCALE_AREA_AVERAGING));

          String destPath = directoryProject + "1conclusioni." + "jpg";
          pathIm1 = destPath;

          upload1.setIcon(tmp);
          photo.add(upload2, "cell 1 0");
          paintAll(getGraphics());
        }
      }
    });
    // foto 2, se seleziono immagine abilito pulsante 3
    upload2.setBorder(null);
    upload2.setContentAreaFilled(false);
    upload2.setRolloverEnabled(true);
    upload2.setRolloverIcon(new ImageIcon(ClassLoader
        .getSystemResource("upload_icon.jpg")));
    upload2.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent arg0) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        ImagePreviewPanel preview = new ImagePreviewPanel();
        chooser.setAccessory(preview);
        chooser.addPropertyChangeListener(preview);
        lastPath = st.getPath();
        if (lastPath.equals(""))
          chooser.getCurrentDirectory();
        else
          chooser.setCurrentDirectory(new File(lastPath));
        int returnValue = chooser.showOpenDialog(upload2);

        if (returnValue == JFileChooser.APPROVE_OPTION) {
          String sourcePath = chooser.getSelectedFile().toString();
          st.setPath(sourcePath);
          lastPath = sourcePath;

          File source = new File(sourcePath);
          sourceIm2 = sourcePath;
          ImageIcon tmp = new ImageIcon(sourcePath);
          tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
              Image.SCALE_AREA_AVERAGING));

          String destPath = directoryProject + "2conclusioni." + "jpg";
          pathIm2 = destPath;

          upload2.setIcon(tmp);
          photo.add(upload3, "cell 2 0");

          paintAll(getGraphics());
        }
      }
    });
    // foto 3, se seleziono immagine abilito pulsante 4
    upload3.setBorder(null);
    upload3.setContentAreaFilled(false);
    upload3.setRolloverEnabled(true);
    upload3.setRolloverIcon(new ImageIcon(ClassLoader
        .getSystemResource("upload_icon.jpg")));
    upload3.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent arg0) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        ImagePreviewPanel preview = new ImagePreviewPanel();
        chooser.setAccessory(preview);
        chooser.addPropertyChangeListener(preview);
        lastPath = st.getPath();
        if (lastPath.equals(""))
          chooser.getCurrentDirectory();
        else
          chooser.setCurrentDirectory(new File(lastPath));
        int returnValue = chooser.showOpenDialog(upload3);

        if (returnValue == JFileChooser.APPROVE_OPTION) {
          String sourcePath = chooser.getSelectedFile().toString();
          st.setPath(sourcePath);
          lastPath = sourcePath;

          File source = new File(sourcePath);
          sourceIm3 = sourcePath;
          ImageIcon tmp = new ImageIcon(sourcePath);
          tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
              Image.SCALE_AREA_AVERAGING));

          String destPath = directoryProject + "3conclusioni." + "jpg";
          pathIm3 = destPath;

          upload3.setIcon(tmp);
          photo.add(upload4, "cell 3 0");
          paintAll(getGraphics());
        }
      }
    });
    // foto 1
    upload4.setBorder(null);
    upload4.setContentAreaFilled(false);
    upload4.setRolloverEnabled(true);
    upload4.setRolloverIcon(new ImageIcon(ClassLoader
        .getSystemResource("upload_icon.jpg")));
    upload4.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent arg0) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        ImagePreviewPanel preview = new ImagePreviewPanel();
        chooser.setAccessory(preview);
        chooser.addPropertyChangeListener(preview);
        lastPath = st.getPath();
        if (lastPath.equals(""))
          chooser.getCurrentDirectory();
        else
          chooser.setCurrentDirectory(new File(lastPath));
        int returnValue = chooser.showOpenDialog(upload4);

        if (returnValue == JFileChooser.APPROVE_OPTION) {
          String sourcePath = chooser.getSelectedFile().toString();
          st.setPath(sourcePath);
          lastPath = sourcePath;

          File source = new File(sourcePath);
          sourceIm4 = sourcePath;
          ImageIcon tmp = new ImageIcon(sourcePath);
          tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
              Image.SCALE_AREA_AVERAGING));

          String destPath = directoryProject + "4conclusioni." + "jpg";
          pathIm4 = destPath;

          upload4.setIcon(tmp);
          paintAll(getGraphics());
        }
      }
    });

    MouseAdapter ml1 = new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON3) {
          int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?",
              "Attenzione", 0);
          if (res == 0) {
            if (!pathIm2.equals("")) {
              // scalo i valori dell'immagine 2 nell'immagine 1
              pathIm1 = pathIm2.replace("2P", "1P");
              sourceIm1 = sourceIm2;
              upload1.setIcon(upload2.getIcon());

              if (!pathIm3.equals("")) {
                // scalo i valori dell'immagine 3 nell'immagine 2
                pathIm2 = pathIm3.replace("3P", "2P");
                sourceIm2 = sourceIm3;
                upload2.setIcon(upload3.getIcon());
                if (!pathIm4.equals("")) {
                  // scalo i valori dell'immagine 4 nell'immagine 3
                  pathIm3 = pathIm4.replace("4P", "3P");
                  sourceIm3 = sourceIm4;
                  // reset immagine 4
                  pathIm4 = "";
                  sourceIm4 = "";
                  upload3.setIcon(upload4.getIcon());
                  upload4.setIcon(new ImageIcon(ClassLoader
                      .getSystemResource("bupload.jpg")));
                } else {
                  // reset immagine 3
                  upload3.setIcon(new ImageIcon(ClassLoader
                      .getSystemResource("bupload.jpg")));
                  pathIm3 = "";
                  sourceIm3 = "";
                  try {
                    photo.remove(upload4);
                  } catch (Exception noRemove) {
                    System.out.println("no remove");
                  }

                }
              } else {
                // reset immagine 2
                upload2.setIcon(new ImageIcon(ClassLoader
                    .getSystemResource("bupload.jpg")));
                pathIm2 = "";
                sourceIm2 = "";
                try {
                  photo.remove(upload3);
                } catch (Exception noRemove) {
                  System.out.println("no remove");
                }

              }
            } else {
              // reset immagine 1
              pathIm1 = "";
              sourceIm1 = "";
              upload1.setIcon(new ImageIcon(ClassLoader
                  .getSystemResource("bupload.jpg")));
              try {
                photo.remove(upload2);
              } catch (Exception noRemove) {
                System.out.println("no remove");
              }
            }
            st.refresh();
          }
        }
      }
    };
    upload1.addMouseListener(ml1);

    MouseAdapter ml2 = new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON3) {
          int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?",
              "Attenzione", 0);
          if (res == 0) {

            if (!pathIm3.equals("")) {
              pathIm2 = pathIm3.replace("3P", "2P");
              sourceIm2 = sourceIm3;
              upload2.setIcon(upload3.getIcon());
              if (!pathIm4.equals("")) {
                pathIm3 = pathIm4.replace("4P", "3P");
                sourceIm3 = sourceIm4;
                pathIm4 = "";
                sourceIm4 = "";
                upload3.setIcon(upload4.getIcon());
                upload4.setIcon(new ImageIcon(ClassLoader
                    .getSystemResource("bupload.jpg")));
              } else {
                upload3.setIcon(new ImageIcon(ClassLoader
                    .getSystemResource("bupload.jpg")));
                pathIm3 = "";
                sourceIm3 = "";
                try {
                  photo.remove(upload4);
                } catch (Exception noRemove) {
                  System.out.println("no remove");
                }
              }
            } else {
              upload2.setIcon(new ImageIcon(ClassLoader
                  .getSystemResource("bupload.jpg")));
              pathIm2 = "";
              sourceIm2 = "";
              try {
                photo.remove(upload3);
              } catch (Exception noRemove) {
                System.out.println("no remove");
              }

            }
            st.refresh();
          }
        }
      }
    };
    upload2.addMouseListener(ml2);

    MouseAdapter ml3 = new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON3) {
          int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?",
              "Attenzione", 0);
          if (res == 0) {

            if (!pathIm4.equals("")) {
              pathIm3 = pathIm4.replace("4P", "3P");
              sourceIm3 = sourceIm4;
              pathIm4 = "";
              sourceIm4 = "";
              upload3.setIcon(upload4.getIcon());
              upload4.setIcon(new ImageIcon(ClassLoader
                  .getSystemResource("bupload.jpg")));
            } else {
              upload3.setIcon(new ImageIcon(ClassLoader
                  .getSystemResource("bupload.jpg")));
              pathIm3 = "";
              sourceIm3 = "";
              try {
                photo.remove(upload4);
              } catch (Exception noRemove) {
                System.out.println("no remove");
              }
            }
            st.refresh();
          }

        }
      }
    };
    upload3.addMouseListener(ml3);

    MouseAdapter ml4 = new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON3) {
          int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?",
              "Attenzione", 0);
          if (res == 0) {
            upload4.setIcon(new ImageIcon(ClassLoader
                .getSystemResource("bupload.jpg")));
            pathIm4 = "";
            sourceIm4 = "";
            st.refresh();
          }

        }
      }
    };
    upload4.addMouseListener(ml4);

    photo.add(infoUpload, "dock north");
    photo.add(upload1, "cell 0 0");

    add(c, "gapleft 15 , wrap");
    add(conclusioni, "gapleft 15 , wrap");
    add(p, "gapleft 15 , wrap");
    add(professionista, "gapleft 15 , wrap");
    add(photo, "gapleft 15");
  }

  public void setTesto() {
    Properties pc = System.getProperties();
    String homePath = pc.getProperty("user.home");
    String file = homePath + pc.getProperty("file.separator")
        + "ArchimedeSuper" + pc.getProperty("file.separator") + "data.txt";
    File rtd = new File(file);
    Scanner c;
    try {
      c = new Scanner(rtd);

      String tmp = c.nextLine();
      nome.setText(tmp);
      tmp = c.nextLine();
      cognome.setText(tmp);
      tmp = c.nextLine();
      cell.setText(tmp);
      tmp = c.nextLine();
      mail.setText(tmp);

    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

  public void resize(File in, int w, int h, String formato, String out)
      throws IOException {
    Image newImg = ImageIO.read(in).getScaledInstance(w, h, Image.SCALE_SMOOTH);
    BufferedImage bim = new java.awt.image.BufferedImage(w, h,
        BufferedImage.TYPE_INT_RGB);
    bim.createGraphics().drawImage(newImg, 0, 0, null);
    FileOutputStream fos = new FileOutputStream(out);
    ImageIO.write(bim, formato, fos);
    fos.close();
  }

  public boolean save() {

    boolean done = true;
    if (!sourceIm1.equals("") && !sourceIm1.equals(pathIm1))
      try {
        resize(new File(sourceIm1), 250, 200, "jpg",
            pathIm1.replace("aaAAzzZZ", st.getProject()));
      } catch (Exception e) {
        sourceIm1 = "";
        pathIm1 = "";
        upload1.setIcon(new ImageIcon(ClassLoader
            .getSystemResource("upload_icon_err.png")));
        done = false;
        e.printStackTrace();
      }
    if (!sourceIm2.equals("") && !sourceIm2.equals(pathIm2))
      try {
        resize(new File(sourceIm2), 250, 200, "jpg",
            pathIm2.replace("aaAAzzZZ", st.getProject()));
      } catch (Exception e) {
        sourceIm2 = "";
        pathIm2 = "";
        upload2.setIcon(new ImageIcon(ClassLoader
            .getSystemResource("upload_icon_err.png")));
        done = false;
        e.printStackTrace();
      }
    if (!sourceIm3.equals("") && !sourceIm3.equals(pathIm3))
      try {
        resize(new File(sourceIm3), 250, 200, "jpg",
            pathIm3.replace("aaAAzzZZ", st.getProject()));
      } catch (Exception e) {
        sourceIm3 = "";
        pathIm3 = "";
        upload3.setIcon(new ImageIcon(ClassLoader
            .getSystemResource("upload_icon_err.png")));
        done = false;
        e.printStackTrace();
      }
    if (!sourceIm4.equals("") && !sourceIm4.equals(pathIm4))
      try {
        resize(new File(sourceIm4), 250, 200, "jpg",
            pathIm4.replace("aaAAzzZZ", st.getProject()));
      } catch (Exception e) {
        sourceIm4 = "";
        pathIm4 = "";
        upload4.setIcon(new ImageIcon(ClassLoader
            .getSystemResource("upload_icon_err.png")));
        done = false;
        e.printStackTrace();
      }

    return done;

  }

  public String print() {

    String print = "\n#" + pathIm1.replace("aaAAzzZZ", st.getProject());
    print = print + "\n#" + pathIm2.replace("aaAAzzZZ", st.getProject());
    print = print + "\n#" + pathIm3.replace("aaAAzzZZ", st.getProject());
    print = print + "\n#" + pathIm4.replace("aaAAzzZZ", st.getProject());
    print = print + "\n" + conclusioni.getText();
    print = print + "\n@@Conclusioni";
    print = print + "\n#" + nome.getText();
    print = print + "\n#" + cognome.getText();
    print = print + "\n#" + cell.getText();
    print = print + "\n#" + mail.getText();
    print = print + "\n@@Professionista";
    return print;
  }

  public void setElement(String data) {
    Scanner c = new Scanner(data);
    c.nextLine();
    sourceIm1 = c.nextLine().substring(1);
    if (sourceIm1.contains("conclusioni")) {

      pathIm1 = directoryProject.replace("aaAAzzZZ", st.getProject())
          + "1conclusioni.jpg";
      sourceIm1 = pathIm1;
      upload1.setIcon(new ImageIcon(pathIm1));
      photo.add(upload2, "cell 1 0");
    } else
      sourceIm1 = "";

    sourceIm2 = c.nextLine().substring(1);
    if (sourceIm2.contains("conclusioni")) {

      pathIm2 = directoryProject.replace("aaAAzzZZ", st.getProject())
          + "2conclusioni.jpg";
      sourceIm2 = pathIm2;
      upload2.setIcon(new ImageIcon(pathIm2));
      photo.add(upload3, "cell 2 0");
      photo.add(upload4, "cell 3 0");
    } else
      sourceIm2 = "";

    sourceIm3 = c.nextLine().substring(1);
    if (sourceIm3.contains("conclusioni")) {
      pathIm3 = directoryProject.replace("aaAAzzZZ", st.getProject())
          + "3conclusioni.jpg";
      sourceIm3 = pathIm3;
      upload3.setIcon(new ImageIcon(pathIm3));
    } else
      sourceIm3 = "";
    sourceIm4 = c.nextLine().substring(1);
    if (sourceIm4.contains("conclusioni")) {
      pathIm4 = directoryProject.replace("aaAAzzZZ", st.getProject())
          + "4conclusioni.jpg";
      sourceIm4 = pathIm4;
      upload4.setIcon(new ImageIcon(pathIm4));
    } else
      sourceIm4 = "";
    String tmp = "";
    while (c.hasNextLine())
      tmp = tmp + "\n" + c.nextLine();
    try {
      conclusioni.setText(tmp.substring(1));
    } catch (Exception e) {
      conclusioni.setText(tmp);
    }
  }

  public void setProfessionista(String data) {
    // System.out.println(data);
    Scanner c = new Scanner(data);
    c.nextLine();
    nome.setText(c.nextLine().substring(1));
    cognome.setText(c.nextLine().substring(1));
    cell.setText(c.nextLine().substring(1));
    mail.setText(c.nextLine().substring(1));
  }

  public Document pdf(Document doc) {
    doc.newPage();
    int col = 0;

    if (!this.pathIm1.equals(""))
      col++;
    if (!this.pathIm2.equals(""))
      col++;
    if (!this.pathIm3.equals(""))
      col++;
    if (!this.pathIm4.equals("")) {
      col++;
    }
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100.0F);

    Chunk underline = new Chunk("10. Conclusioni", FontFactory.getFont("Times",
        15.0F, 1));
    underline.setUnderline(1.2F, -2.0F);

    Anchor anchorTarget = new Anchor(underline);
    anchorTarget.setName("conclusioni");
    Paragraph targetParagraph = new Paragraph();
    targetParagraph.add(anchorTarget);
    try {
      doc.add(targetParagraph);
    } catch (DocumentException e2) {
      e2.printStackTrace();
    }

    underline = new Chunk(this.conclusioni.getText(), FontFactory.getFont(
        "Times", 11.0F));
    PdfPCell cos = new PdfPCell(new Phrase(underline));
    cos.setColspan(2);
    cos.setHorizontalAlignment(Element.ALIGN_LEFT);
    cos.setVerticalAlignment(Element.ALIGN_TOP);
    cos.setBorder(PdfPCell.NO_BORDER);
    switch (col) {
    case 0:
      cos.setMinimumHeight(450);
      break;
    case 1:
    case 2:
      cos.setMinimumHeight(230);
      break;
    default:
      cos.setMinimumHeight(130);
      break;
    }
    table.addCell(cos);

    try {
      table.setSpacingBefore(10.0F);
      doc.add(table);
    } catch (DocumentException e) {
      e.printStackTrace();
    }
    table = new PdfPTable(2);
    table.setWidthPercentage(100.0F);
    table.setKeepTogether(true);

    if (!pathIm1.equals(""))
      try {
        com.itextpdf.text.Image img = com.itextpdf.text.Image
            .getInstance(pathIm1.replace("aaAAzzZZ", st.getProject()));
        img.setCompressionLevel(9);
        if (col < 2)
          img.scaleAbsolute(265f, 210f);
        else
          img.scaleAbsolute(250f, 175f);

        cos = new PdfPCell(img);
        if (col == 1) {
          cos.setColspan(2);
        }
        cos.setHorizontalAlignment(Element.ALIGN_LEFT);
        cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cos.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cos);
      } catch (BadElementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    if (!pathIm2.equals(""))
      try {
        com.itextpdf.text.Image img = com.itextpdf.text.Image
            .getInstance(pathIm2.replace("aaAAzzZZ", st.getProject()));
        img.setCompressionLevel(9);
        if (col < 2)
          img.scaleAbsolute(265f, 210f);
        else
          img.scaleAbsolute(250f, 175f);

        cos = new PdfPCell(img);
        cos.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cos.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cos);
      } catch (BadElementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    if (!pathIm3.equals(""))
      try {
        com.itextpdf.text.Image img = com.itextpdf.text.Image
            .getInstance(pathIm3.replace("aaAAzzZZ", st.getProject()));
        img.setCompressionLevel(9);
        Paragraph dati = new Paragraph("");
        PdfPCell space = new PdfPCell(dati);
        space.setBorder(PdfPCell.NO_BORDER);
        table.addCell(space);
        table.addCell(space);
        if (col < 2)
          img.scaleAbsolute(265f, 210f);
        else
          img.scaleAbsolute(250f, 175f);

        cos = new PdfPCell(img);
        if (col == 3)
          cos.setColspan(2);
        cos.setHorizontalAlignment(Element.ALIGN_LEFT);
        cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cos.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cos);
      } catch (BadElementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    if (!pathIm4.equals(""))
      try {
        com.itextpdf.text.Image img = com.itextpdf.text.Image
            .getInstance(pathIm4.replace("aaAAzzZZ", st.getProject()));
        img.setCompressionLevel(9);
        if (col < 2)
          img.scaleAbsolute(265f, 210f);
        else
          img.scaleAbsolute(250f, 175f);
        cos = new PdfPCell(img);
        cos.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cos.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cos);
      } catch (BadElementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

    cos = new PdfPCell(new Paragraph(
        "Rimango a vostra disposizione per ulteriori chiarimenti",
        FontFactory.getFont("Times", 13.0F)));
    cos.setBorder(0);
    cos.setColspan(2);
    cos.setFixedHeight(20);
    cos.setVerticalAlignment(PdfPCell.ALIGN_BOTTOM);
    table.addCell(cos);

    cos = new PdfPCell(new Paragraph("Cordiali saluti\n" + this.nome.getText()
        + " " + this.cognome.getText() + "\n" + this.cell.getText() + "\n"
        + this.mail.getText(), FontFactory.getFont("Times", 13.0F)));
    cos.setBorder(0);
    table.addCell(cos);
    try {
      com.itextpdf.text.Image img = com.itextpdf.text.Image
          .getInstance(ClassLoader.getSystemResource("logo_eureka_blu.jpg"));
      img.scaleAbsolute(130.0F, 130.0F);
      cos = new PdfPCell(img);
      cos.setBorder(0);
      cos.setHorizontalAlignment(2);
      table.addCell(cos);
    } catch (BadElementException e) {
      e.printStackTrace();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    try {
      table.setSpacingBefore(10.0F);
      doc.add(table);
    } catch (DocumentException e) {
      e.printStackTrace();
    }

    return doc;
  }

  public void replace(String newName, String oldName) {
    sourceIm1 = pathIm1;
    sourceIm2 = pathIm2;
    sourceIm3 = pathIm3;
    sourceIm4 = pathIm4;
    pathIm1 = pathIm1.replace(oldName, "aaAAzzZZ");
    pathIm2 = pathIm2.replace(oldName, "aaAAzzZZ");
    pathIm3 = pathIm3.replace(oldName, "aaAAzzZZ");
    pathIm4 = pathIm4.replace(oldName, "aaAAzzZZ");
  }
}
